{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[False False False  True False]\n",
      " [ True False False  True False]\n",
      " [ True False False  True  True]\n",
      " [ True False False  True  True]\n",
      " [ True False False  True  True]\n",
      " [ True False  True  True  True]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr = [3, 34, 4, 12, 5, 2]\n",
    "\n",
    "def rec_sub(arr, i, s): \n",
    "    if s == 0:\n",
    "        print (\"s:\"+str(s) + \" arr:\"+str(arr[i+1]))\n",
    "        return True\n",
    "    if s < 0 :\n",
    "        return False\n",
    "    if i == 0:\n",
    "        return s == arr[0]\n",
    "    elif s == arr[i]:\n",
    "        return True\n",
    "    else :\n",
    "        A = rec_sub(arr, i - 1, s - arr[i])\n",
    "        B = rec_sub(arr, i - 1, s)\n",
    "        return A or B \n",
    "    \n",
    "rec_sub(arr, 5, 9)\n",
    "\n",
    "def dp_sub(arr, S):\n",
    "    subset = np.zeros((len(arr), S + 1), dtype = bool)\n",
    "    subset[:, 0] = True\n",
    "    subset[0, :] = False\n",
    "\n",
    "    if(arr[0] <= S):\n",
    "        subset[0, arr[0]] = True\n",
    "    \n",
    "    for i in range(1, len(arr)):\n",
    "        for s in range (1, S + 1):\n",
    "            if arr[i] > s:\n",
    "                subset[i, s] = subset[i - 1, s]\n",
    "            else:\n",
    "                A = subset[i - 1, s - arr[i]]\n",
    "                B = subset[i - 1, s]\n",
    "                subset[i, s] = A or B\n",
    "    r,c = subset.shape\n",
    "    print(subset)\n",
    "    return subset[r - 1, c - 1]\n",
    "\n",
    "dp_sub(arr, 4)\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
